<?php
namespace App\Api\Controllers\CmbcUnion;

use App\Api\Controllers\CmbcUnion\BaseController as base;
use Illuminate\Support\Facades\Log;
use function EasyWeChat\Kernel\Support\get_client_ip;

class PayController extends BaseController
{

    public function scan_pay($data)
    {
        try {
            $base = base::getInstance();
            $head = [
                'platmerid' => $data['platform_id'],
                'trancode'  => 'CAS016',
                'tranflow'  => $data['platform_id'].'-'.uniqid(),
            ];
            $body = [
                'merid'     => $data['mch_id'],
                'paytype'   => $data['paytype'],
                'tranamt'   => $data['total_amount'],
                'authcode'   => $data['authcode'],
                'serverurl' => url('/api/cmbcunion/pay_notify_url'),
                'extendfield' => $data['out_trade_no'],
                'terminal_info' => $data['terminal_info'],
                'spbillcreateip' => get_client_ip(),
                'productlist' => json_encode([
                    'productlist' => [
                        [
                            'productname' => '商品',
                            'productnum' => 1,
                            'productamt' => $data['total_amount'],
                        ],
                    ]
                ])
            ];
            if (isset($data['goodsdetail']) && !empty($data['goodsdetail'])) {
                $body['goodsdetail'] = $data['goodsdetail'];
            }

            $ret  = json_decode($this->params($body, $head)->build($base::MIAN, 'scan_pay'), true);

            if (isset($ret['message']['data']['head']['respcode']) && !empty($ret['message']['data']['head']['respcode'])
                && $ret['message']['data']['head']['respcode'] == 'C000000000'
            ) {
                return [
                    'status' => 1,
                    'message' => '支付成功',
                    'data' => $ret['message']['data']
                ];
            } else  if (isset($ret['message']['data']['head']['respcode']) && !empty($ret['message']['data']['head']['respcode'])
                && $ret['message']['data']['head']['respcode'] == 'W000000000'
            ) {
                return [
                    'status' => 2,
                    'message' => '支付中',
                ];
            }else {
                $message = '失败了';
                if (isset($ret['message']['data']['head']['respmsg'])) {
                    $message = $ret['message']['data']['head']['respmsg'];
                }
                return [
                    'status' => 3,
                    'message' => $message,
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }


    //查询订单 0-系统错误 1-成功 2-正在支付 3-失败 4.已经退款

    public function order_query($data, $out_trade_no)
    {
        try {
            $base = base::getInstance();
            $head = [
                'platmerid' => $data['platform_id'],
                'trancode'  => 'CAS008',
                'tranflow'  => $data['platform_id'].'-'.uniqid(),
            ];
            $body = [
                'oritrancode'   => 'CAS016',
                'oritranflow'   => $data['tranflow'],
            ];

            $ret  = json_decode($this->params($body, $head)->build($base::MIAN, 'order_query'), true);

            if (isset($ret['message']['data']['body']['orirespcode']) && !empty($ret['message']['data']['body']['orirespcode'])
                && $ret['message']['data']['body']['orirespcode'] == 'C000000000'
            ) {
                return [
                    'status' => 1,
                    'message' => '支付成功',
                    'data' => $ret['message']['data']
                ];
            } else  if (isset($ret['message']['data']['body']['orirespcode']) && !empty($ret['message']['data']['body']['orirespcode'])
                && $ret['message']['data']['body']['orirespcode'] == 'W000000000'
            ) {
                return [
                    'status' => 2,
                    'message' => '支付中',
                ];
            }else {
                $message = '失败了';
                if (isset($ret['message']['data']['body']['orirespmsg'])) {
                    $message = $ret['message']['data']['body']['orirespmsg'];
                }
                return [
                    'status' => 3,
                    'message' => $message,
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }


    //退款 0-系统错误 1-成功
    public function refund($data)
    {
        try {
            $base = base::getInstance();
            $head = [
                'platmerid' => $data['platform_id'],
                'trancode'  => 'CAS006',
                'tranflow'  => $data['platform_id'].'-'.uniqid(),
            ];
            $body = [
                'merid'   => $data['mch_id'],
                'oritranflow'   => $data['out_trade_no'],
                'refundamt' => $data['refund_amount'],
                'extendfield' => $data['refund_no'],
                'remark' => '退款'
            ];

            $ret  = json_decode($this->params($body, $head)->build($base::MIAN, 'refund'), true);

            if (isset($ret['message']['data']['head']['respcode']) && !empty($ret['message']['data']['head']['respcode'])
                && $ret['message']['data']['head']['respcode'] == 'C000000000'
            ) {
                if ($ret['message']['data']['body']['status'] == '01') {
                    return [
                        'status' => 2,
                        'message' => '退款中',
                        'data' => $ret['message']['data']
                    ];
                }
                if ($ret['message']['data']['body']['status'] == '40') {
                    \App\Common\PaySuccessAction::addCoupon($data['out_trade_no'], '', 'refund');
                    return [
                        'status' => 1,
                        'message' => '成功',
                        'data' => $ret['message']['data']
                    ];
                }
                if ($ret['message']['data']['body']['status'] == '41') {
                    return [
                        'status' => 3,
                        'message' => '退款失败',
                    ];
                }

            }else {
                $message = '失败了';
                if (isset($ret['message']['data']['head']['respmsg'])) {
                    $message = $ret['message']['data']['head']['respmsg'];
                }
                return [
                    'status' => 3,
                    'message' => $message,
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 0,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }


    //退款查询 0-系统错误 1-成功 2-正在退款 3-失败
    public function refund_query($data)
    {
        try {


        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }


    public function qr_submit ($data)
    {
        try {
            $base = base::getInstance();
            $head = [
                'platmerid' => $data['platform_id'],
                'trancode'  => 'CAS016',
                'tranflow'  => $data['platform_id'].'-'.uniqid(),
            ];
            $body = [
                'merid'     => $data['mch_id'],
                'paytype'   => $data['paytype'],
                'tranamt'   => $data['total_amount'],
                'serverurl' => url('/api/cmbcunion/pay_notify_url'),
                'subappid'  => '',
                'extendfield' => $data['out_trade_no'],
                'openid'      => $data['openid'],
                'spbillcreateip' => get_client_ip(),
                'productlist' => $data['goods_detail']
            ];

            $ret  = json_decode($this->params($body, $head)->build($base::MIAN, 'qr_submit'), true);

            if (isset($ret['message']['data']['body']['payinfo']) && !empty($ret['message']['data']['body']['payinfo'])) {
                $payinfo = $ret['message']['data']['body']['payinfo'];
                if ($data['paytype'] == '05') {
                    $wxJosn = json_decode($payinfo,true);
                    $retData = [
                        'payTimeStamp' => $wxJosn['timeStamp'],
                        'payPackage' => $wxJosn['package'],
                        'paySign' => $wxJosn['paySign'],
                        'payAppId' => $wxJosn['appId'],
                        'paynonceStr' => $wxJosn['nonceStr'],
                        'paySignType' => $wxJosn['signType'],
                    ];
                    return [
                        'status' => 1,
                        'message' => '',
                        'data' => $retData
                    ];
                }
                if ($data['paytype'] == '06') {
                    //支付宝
                    $payinfo = $ret['message']['data']['body']['payinfo'];
                    //支付宝
                    return [
                        'status' => 1,
                        'message' => '成功',
                        'data' => [
                            'codeUrl' => $payinfo
                        ]
                    ];
                }
                if ($data['paytype'] == '12') {
                    //云闪付
                }
                return [
                    'status' => 3,
                    'message' => '系统异常',
                ];
            } else {
                $message = '失败了';
                if (isset($ret['message']['data']['head']['respmsg'])) {
                    $message = $ret['message']['data']['head']['respmsg'];
                }
                return [
                    'status' => 3,
                    'message' => $message,
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }





}
